package com.mindskip.xzs;

import com.mindskip.xzs.configuration.property.SystemConfig;
import com.mindskip.xzs.domain.ExamPaper;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.service.ExamPaperService;
import com.mindskip.xzs.service.QuestionService;
import com.mindskip.xzs.service.TaskExamService;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamPaperTitleItemVM;
import com.mindskip.xzs.viewmodel.admin.exam.ExamResponseVM;
import com.mindskip.xzs.viewmodel.admin.question.QuestionEditRequestVM;
import com.mindskip.xzs.viewmodel.admin.task.TaskRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author ThreeGlods
 */
@SpringBootApplication
@EnableTransactionManagement
@EnableConfigurationProperties(value = {SystemConfig.class})
@EnableCaching
public class XzsApplication {

    public static ExamPaperService examPaperService;

    public static QuestionService questionService;

    public static TaskExamService taskExamService;

    public XzsApplication(ExamPaperService examPaperService, QuestionService questionService,TaskExamService taskExamService) {
        this.examPaperService = examPaperService;
        this.questionService = questionService;
        this.taskExamService = taskExamService;
    }

    public static void main(String[] args) {
        SpringApplication.run(XzsApplication.class, args);

        //线程
        Runnable runnable = () -> run(examPaperService);
        ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
        //public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
        //command--执行的任务,initialDelay--延迟开始,period--间隔时间,unit--时间单位
        service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.MINUTES);
    }

    @Autowired
    private static void run(ExamPaperService examPaperService) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        SimpleDateFormat sdf2 = new SimpleDateFormat("MM月dd日");

        //    && sdf.format(new Date()).equals("17:07:15")
        int a = run01()*5+315;
        if (sdf.format(new Date()).equals("00:01")) {
            try {
                Integer integer = questionService.selectAllCount();
                if (a+5 <= integer) {
                    ArrayList<QuestionEditRequestVM> questions = new ArrayList<>();
                    for (int i = a; i < a+5; i++) {
                        QuestionEditRequestVM requestVM = questionService.getQuestionEditRequestVM(i);
                        questions.add(0, requestVM);
                    }
                    ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM();
                    examPaperTitleItemVM.setName("一");
                    examPaperTitleItemVM.setQuestionItems(questions);

                    ArrayList<ExamPaperTitleItemVM> titleItemVMS = new ArrayList<>();
                    titleItemVMS.add(0, examPaperTitleItemVM);

                    ExamPaperEditRequestVM model = new ExamPaperEditRequestVM();
                    model.setLevel(1);
                    model.setSubjectId(1);
                    model.setPaperType(6);
                    model.setName(sdf2.format(new Date())+"消防救援队伍灭火每日答题");
                    model.setSuggestTime(10);
                    model.setTitleItems(titleItemVMS);

                    User user = new User();
                    user.setId(2);
                    examPaperService.savePaperFromVM(model, user);

                    //创建自动任务
                    ExamPaper examPaper = examPaperService.selectById();
                    ExamResponseVM examResponseVM = new ExamResponseVM();
                    examResponseVM.setId(examPaper.getId());
                    examResponseVM.setName(examPaper.getName());
                    examResponseVM.setCreateUser(examPaper.getCreateUser());
                    examResponseVM.setPaperType(examPaper.getPaperType());
                    examResponseVM.setFrameTextContentId(examPaper.getFrameTextContentId());
                    examResponseVM.setQuestionCount(examPaper.getQuestionCount());
                    examResponseVM.setScore(examPaper.getScore());
                    examResponseVM.setSubjectId(examPaper.getSubjectId());
                    examResponseVM.setCreateTime(DateTimeUtil.dateFormat(examPaper.getCreateTime()));

                    ArrayList<ExamResponseVM> examResponseVMS = new ArrayList<>();
                    examResponseVMS.add(0,examResponseVM);

                    TaskRequestVM vm = new TaskRequestVM();
                    vm.setGradeLevel(1);

                    vm.setTitle(sdf2.format(new Date())+"答题");
                    vm.setPaperItems(examResponseVMS);
                    taskExamService.edit(vm, user);
                    taskExamService.taskExamToVM(vm.getId());

                }else {
                    a = (int) (1 + Math.random() * (integer - 5 + 1));

                    ArrayList<QuestionEditRequestVM> questions = new ArrayList<>();
                    for (int i = a; i < a+5; i++) {
                        QuestionEditRequestVM requestVM = questionService.getQuestionEditRequestVM(i);
                        questions.add(0, requestVM);

                    }
                    ExamPaperTitleItemVM examPaperTitleItemVM = new ExamPaperTitleItemVM();
                    examPaperTitleItemVM.setName("一");
                    examPaperTitleItemVM.setQuestionItems(questions);

                    ArrayList<ExamPaperTitleItemVM> titleItemVMS = new ArrayList<>();
                    titleItemVMS.add(0, examPaperTitleItemVM);

                    ExamPaperEditRequestVM model = new ExamPaperEditRequestVM();
                    model.setLevel(1);
                    model.setSubjectId(1);
                    model.setPaperType(6);
                    model.setName(sdf2.format(new Date())+"消防救援队伍灭火每日答题");
                    model.setSuggestTime(10);
                    model.setTitleItems(titleItemVMS);

                    User user = new User();
                    user.setId(2);
                    examPaperService.savePaperFromVM(model, user);
    //创建自动任务
                    ExamPaper examPaper = examPaperService.selectById();
                    ExamResponseVM examResponseVM = new ExamResponseVM();
                    examResponseVM.setId(examPaper.getId());
                    examResponseVM.setName(examPaper.getName());
                    examResponseVM.setCreateUser(examPaper.getCreateUser());
                    examResponseVM.setPaperType(examPaper.getPaperType());
                    examResponseVM.setFrameTextContentId(examPaper.getFrameTextContentId());
                    examResponseVM.setQuestionCount(examPaper.getQuestionCount());
                    examResponseVM.setScore(examPaper.getScore());
                    examResponseVM.setSubjectId(examPaper.getSubjectId());
                    examResponseVM.setCreateTime(DateTimeUtil.dateFormat(examPaper.getCreateTime()));

                    ArrayList<ExamResponseVM> examResponseVMS = new ArrayList<>();
                    examResponseVMS.add(0,examResponseVM);

                    TaskRequestVM vm = new TaskRequestVM();
                    vm.setGradeLevel(1);
                    vm.setTitle(sdf2.format(new Date())+"答题");
                    vm.setPaperItems(examResponseVMS);
                    taskExamService.edit(vm, user);
                    taskExamService.taskExamToVM(vm.getId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("当前的系统时间为：" + sdf.format(new Date()));
    }


    //计算相差天数得到每天增长的数值
    private static int run01() {
            int a =0;
            String dateStr2 = "2021-04-01";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date date1 = sdf.parse(dateStr2);
                Date date2 = new Date();
                a = differentDaysByMillisecond(date1, date2);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return a;
    }

    private static int differentDaysByMillisecond(Date date1,Date date2) {
        int days = (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));
        return days;
    }
}

